package com.beiming.libra.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import com.beiming.libra.common.Assert;
import com.beiming.libra.common.constants.ValidateMessage;
import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.common.enums.StatusEnum;
import com.beiming.libra.common.page.Page;
import com.beiming.libra.common.page.PageResult;
import com.beiming.libra.dao.DictionaryMapper;
import com.beiming.libra.domain.Dictionary;
import com.beiming.libra.domain.dto.requestdto.DictionaryRequestDTO;
import com.beiming.libra.service.DictionaryService;


/***
 * @author weibo
 * */
@Service
@Slf4j
public class DictionaryServiceImpl implements DictionaryService {
  @Resource
  private DictionaryMapper dictionaryMapper;

  @Override
  public Dictionary getDictionary(Long dictionaryId) {
    Assert.isNotNull(dictionaryId, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    Dictionary dictionary = dictionaryMapper.selectByPrimaryKey(dictionaryId);
    return dictionary;
  }

  @Override
  public void insertDictionary(DictionaryRequestDTO dictionaryRequestDTO) {
    List<Dictionary> list =
        dictionaryMapper.listDictionary("", dictionaryRequestDTO.getCode(), -1, -1);
    Assert.isListEmpty(list, ErrorCode.DICTIONARY_CODE_EXITS);
    Dictionary dictionary = new Dictionary();
    dictionary.setParentCode(dictionaryRequestDTO.getParentCode());
    dictionary.setCode(dictionaryRequestDTO.getCode());
    dictionary.setValue(dictionaryRequestDTO.getValue());
    dictionary.setRemark(dictionaryRequestDTO.getRemark());
    dictionary.setStatus(StatusEnum.USED.getCode());
    dictionary.setCreateTime(new Date());
    dictionary.setCreateUser(dictionaryRequestDTO.getCreateUser());
    dictionary.setVersion(0);
    dictionaryMapper.insert(dictionary);
  }

  /**
   * 字典列表
   */
  @SuppressWarnings("unchecked")
  @Override
  public PageResult<Dictionary> listDictionary(DictionaryRequestDTO dictionaryRequestDTO) {
    List<Dictionary> list =
        dictionaryMapper.listDictionary(dictionaryRequestDTO.getParentCode(),
            dictionaryRequestDTO.getCode(), (dictionaryRequestDTO.getPageNo() - 1)
                * dictionaryRequestDTO.getPageSize(), dictionaryRequestDTO.getPageSize());
    PageResult<Dictionary> pageResult = new PageResult<Dictionary>();
    pageResult.setList(list);
    int total =
        dictionaryMapper.listDictionaryCount(dictionaryRequestDTO.getParentCode(),
            dictionaryRequestDTO.getCode());
    Page page = new Page();
    page.setPageNo(dictionaryRequestDTO.getPageNo());
    page.setPageSize(dictionaryRequestDTO.getPageSize());
    page.setTotalRecords(total);
    pageResult.setPage(page);
    return pageResult;
  }

  /** 更新字典 */
  @Override
  public void updateDictionary(DictionaryRequestDTO dictionaryRequestDTO) {
    Long dictionaryId = dictionaryRequestDTO.getId();
    Assert.isNotNull(dictionaryId, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    // 更新。如果code存在，则需要校验code是否已经存在
    if (!StringUtils.isEmpty(dictionaryRequestDTO.getCode())) {
      List<Dictionary> list =
          dictionaryMapper.listDictionary("", dictionaryRequestDTO.getCode(), -1, -1);
      Assert.isListEmpty(list, ErrorCode.DICTIONARY_CODE_EXITS);
    }
    Dictionary dictionary = dictionaryMapper.selectByPrimaryKey(dictionaryId);
    Assert.isNotNull(dictionary, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_EXISTS);

    if (!StringUtils.isEmpty(dictionaryRequestDTO.getParentCode())) {
      dictionary.setParentCode(dictionaryRequestDTO.getParentCode());
    }
    if (!StringUtils.isEmpty(dictionaryRequestDTO.getCode())) {
      dictionary.setCode(dictionaryRequestDTO.getCode());
    }
    if (!StringUtils.isEmpty(dictionaryRequestDTO.getValue())) {
      dictionary.setValue(dictionaryRequestDTO.getValue());
    }
    if (!StringUtils.isEmpty(dictionaryRequestDTO.getRemark())) {
      dictionary.setRemark(dictionaryRequestDTO.getRemark());
    }
    dictionary.setUpdateUser(dictionaryRequestDTO.getUpdateUser());
    dictionary.setUpdateTime(new Date());
    dictionaryMapper.updateByPrimaryKey(dictionary);
  }

  @Override
  public void deleteDictionary(Long dictionaryId) {
    Assert.isNotNull(dictionaryId, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    Dictionary dictionary = dictionaryMapper.selectByPrimaryKey(dictionaryId);
    Assert.isNotNull(dictionary, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_EXISTS);
    Example example = new Example(Dictionary.class, false);
    example.createCriteria().andEqualTo("id", dictionaryId);
    dictionaryMapper.deleteByExample(example);
  }

  @Override
  public List<Dictionary> getDictionaryByParentCode(String parentCode) {
    if (StringUtils.isEmpty(parentCode))
      return new ArrayList<Dictionary>();
    Example example = new Example(Dictionary.class, false);
    example.createCriteria().andEqualTo("parentCode", parentCode);
    return dictionaryMapper.selectByExample(example);
  }
}
